﻿using System;
using System.Collections.Generic;
using System.Data;
using CoreSoft.Frame.Entity;
using CoreSoft.Frame.Entity.http;
using U8API.Pub;
using U8Login;
using UFIDA.U8.MomServiceCommon;

namespace U8API.Plugin
{
    public class CustomerclassPlugin
    {
        //private readonly string _ip = ConfigurationManager.AppSettings["WmsIP"] + "Customerclassapi";
        public static string constr = "";
        private string url = "";

        private String GetCustJs(clsLogin login, MSXML2.IXMLDOMDocument2 domhead, int operation)
        {
            var datatable = Common.GetUrl(constr, login.get_cAcc_Id()).Tables[0];
            var datarow = datatable.Rows[0];
            url = datarow[1] + "Customerclassapi";
            //var datatabledept = Common.GetUser(constr).Tables[0];
            //DebugWriteLog.WriteLine("cAcc_Id:" + login.get_cAcc_Id() + ",count:" + datatabledept.Rows.Count);
            //bool hasdept = false;
            //foreach (DataRow item in datatabledept.Rows)
            //{
            //    //部门code
            //    var deptcode = item[1].ToString();
            //    //过滤如果不是QZ不触发插件
            //    if (deptcode == login.get_cAcc_Id())
            //    {
            //        DebugWriteLog.WriteLine("cAcc_Id:" + login.get_cAcc_Id() + ",deptcode:" + deptcode);
            //        hasdept = true;
            //    }
            //}
            //if (!hasdept)//限制
            //{
            //    return "Nopermission";
            //}
            //MSXML2.IXMLDOMNode tmpNode = domhead.selectSingleNode("/customer");
            //String custcode = tmpNode.attributes.getNamedItem("ccuscode").nodeValue.ObjToString();
            //String custname = tmpNode.attributes.getNamedItem("ccusname").nodeValue.ObjToString();
            //string ccode = tmpNode.attributes.getNamedItem("ccccode").nodeValue.ObjToString();

            String ccode = domhead.selectSingleNode("//ccccode ").nodeTypedValue.ObjToString();
            List<Customerclass> custdatas = new List<Customerclass>();
            if (operation != 1) //不是删除
            {
                String cname = domhead.selectSingleNode("//cccname").nodeTypedValue.ObjToString();

                //string cgrade = domhead.selectSingleNode("//iccgrade").nodeTypedValue.ObjToString();
                Int32 StartFlag = 1;

                //父级分类
                String parentCode = "";
                int cnamelength = ccode.Length;
                if (cnamelength > 2)
                {
                    parentCode = ccode.Substring(0, cnamelength - 2);
                }
                else // 没有父级
                {
                    parentCode = "";
                }


                
                Customerclass data = new Customerclass();
                data.CustomerCCode = ccode;
                data.CustomerCName = cname;
                data.ParentCode = parentCode;
                data.StartFlag = StartFlag;
                custdatas.Add(data);
            }
            else
            {
                custdatas.Add(new Customerclass() { CustomerCCode = ccode });
            }
            ApiRequest req = new ApiRequest();
            req.data = custdatas;
            req.orgcode = login.get_cAcc_Id();
            req.primarykey = ccode;
            req.operation = operation;
            DebugWriteLog.WriteLine(req.ToJson());
            return req.ToJson();
        }
        public bool Update_After(ref MSXML2.IXMLDOMDocument2 domhead, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 2);
                //不是qz直接返回true
                if (js == "Nopermission")
                {
                    return true;
                }
                var result = HttpHelper.PostDataToServer(url, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
        public bool Insert_After(ref MSXML2.IXMLDOMDocument2 domhead, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 0);
                //不是qz直接返回true
                if (js == "Nopermission")
                {
                    return true;
                }
                var result = HttpHelper.PostDataToServer(url, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
        public bool Delete_After(ref MSXML2.IXMLDOMDocument2 domhead, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 1);
                //不是qz直接返回true
                if (js == "Nopermission")
                {
                    return true;
                }
                var result = HttpHelper.PostDataToServer(url, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
    }
}
